package com.budwk.app.sqfwV2.service.patrolAndVisit;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.constants.SqfwFormSummaryConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwCompleteflowService;
import com.budwk.app.sqfw.services.SqfwRallyService;
import com.budwk.app.sqfw.services.SqfwServicerecordService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowPdfParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.pdfService.CompleteFlowCommonPdfService;
import com.budwk.app.sqfwDoc.codeGeneratorV2.result.GeneratorPdfWordResult;
import com.budwk.app.sqfwV2.dto.PropagandaScreeningDTO;
import com.budwk.app.sqfwV2.dto.dto.AuthenticRightDTO;
import com.budwk.app.sqfwV2.dto.dulaying.ScreeningHiddenDTO;
import com.budwk.app.sqfwV2.enums.PatrolAndVisitEnum;
import com.budwk.app.sqfwV2.generator.baiyunqv.AuthenticRightPdfGenerator;
import com.budwk.app.sqfwV2.generator.patrolAndVisit.PatrolAndVisitPdfGenerator;
import com.google.common.collect.Lists;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@IocBean
public class PatrolAndVisitPdfService extends CompleteFlowCommonPdfService {

    @Inject
    SqfwCompleteflowService sqfwCompleteflowService;

    @Inject
    SqfwServicerecordService sqfwServicerecordService;

    @Override
    public void createPdfAndMsg(CompleteFlowPdfParamForController paramForController) {
        super.createPdfAndMsg(paramForController);
    }


    @Override
    public GeneratorPdfWordResult createPdfSync(CompleteFlowPdfParamForController paramForController) {
        return super.createPdfSync(paramForController);
    }

    /**
     * 获取用户自定义字段 代码生成器已经自动填充
     * 无需特殊修改
     *
     * @param paramData json字符串
     * @return 结果
     */
    @Override
    protected AbstractCustomerPdfCreateField getCustomerPdfField(String paramData) {
        return JSONObject.parseObject(paramData, PropagandaScreeningDTO.class);
    }

    /**
     * 允许用户自定义填充在创建pdf时所提供的数据
     *
     * @param pdfGenerateParam 创建pdf所提供的数据
     */
    @Override
    protected void customerPopularPdfGenerateParam(PdfGenerateParam pdfGenerateParam) {


    }

    /**
     * 在这里设置生成pdf的所有generator
     *
     * @param pdfGenerateParam 创建pdf的参数
     * @return 生成pdf的generator合集
     */
    @Override
    protected List<BasePdfGenerator> getPdfGenerator(PdfGenerateParam pdfGenerateParam) {
        List<BasePdfGenerator> generatorList = Lists.newArrayList();
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        Sqfw_formSummary_new formSummary = businessParam.getFormSummary();
        PropagandaScreeningDTO propagandaScreeningDTO = JSON.parseObject(formSummary.getParamData(), PropagandaScreeningDTO.class);
        if (propagandaScreeningDTO.getType().equals(PatrolAndVisitEnum.C.getName())) {
            PatrolAndVisitPdfGenerator patrolAndVisitPdfGenerator = new PatrolAndVisitPdfGenerator(PatrolAndVisitEnum.getPdfTemplatName(propagandaScreeningDTO.getType()));
            patrolAndVisitPdfGenerator.setPropagandaInvestigationAndSummary(propagandaScreeningDTO.getPropagandaInvestigationAndSummary());
            generatorList.add(patrolAndVisitPdfGenerator);

        } else if(propagandaScreeningDTO.getType().equals(PatrolAndVisitEnum.A.getName())) {
            List<String> themeList = Arrays.asList(propagandaScreeningDTO.getTheme().split(","));
            for (String theme : themeList) {
                PatrolAndVisitPdfGenerator patrolAndVisitPdfGenerator = new PatrolAndVisitPdfGenerator(PatrolAndVisitEnum.getPdfTemplatName(propagandaScreeningDTO.getType()));
                patrolAndVisitPdfGenerator.setTheme(theme);
                Map<String, String> map =  JSONUtil.toBean(propagandaScreeningDTO.getPropagandaInvestigationAndSummary(),Map.class);
                patrolAndVisitPdfGenerator.setPropagandaInvestigationAndSummary(map.get(theme));
                generatorList.add(patrolAndVisitPdfGenerator);
            }

        }else if(propagandaScreeningDTO.getType().equals(PatrolAndVisitEnum.B.getName())) {
           if(CollectionUtil.isNotEmpty(propagandaScreeningDTO.getScreeningHiddenList())){
               for (ScreeningHiddenDTO screeningHiddenDTO : propagandaScreeningDTO.getScreeningHiddenList()) {
                   PatrolAndVisitPdfGenerator patrolAndVisitPdfGenerator = new PatrolAndVisitPdfGenerator(PatrolAndVisitEnum.getPdfTemplatName(propagandaScreeningDTO.getType()));
                   patrolAndVisitPdfGenerator.setTheme(screeningHiddenDTO.getTheme());
                   patrolAndVisitPdfGenerator.setScreeningSummarize(screeningHiddenDTO.getScreeningSummarize());
                   generatorList.add(patrolAndVisitPdfGenerator);
               }
           }

        }
        return generatorList;
    }

    /**
     * 用户自定义下载pdf文件的名称
     *
     * @param pdfGenerateParam 创建pdf所提供的参数
     * @return 结果
     */
    @Override
    protected String getDownloadFileName(PdfGenerateParam pdfGenerateParam) {
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        Sqfw_formSummary_new formSummary = businessParam.getFormSummary();
        PropagandaScreeningDTO propagandaScreeningDTO = JSON.parseObject(formSummary.getParamData(), PropagandaScreeningDTO.class);
        return StrUtil.format(PatrolAndVisitEnum.getFileName(propagandaScreeningDTO.getType()));
    }

    /**
     * 第五张表中的需要的类型，详情见 {@link com.budwk.app.sqfw.constants.SqfwFormSummaryConstants}
     *
     * @return 返回数据
     */
    @Override
    public String getSummaryType() {
        return SqfwFormSummaryConstants.propaganda_screening;
    }


    protected void beforeInsertForSummary(Sqfw_formSummary_new formSummary) {
        PropagandaScreeningDTO duLaYingPropagandaOrScreeningPdfDTO= JSONObject.parseObject(formSummary.getParamData(), PropagandaScreeningDTO.class);
        if(cn.hutool.core.util.StrUtil.isNotEmpty(duLaYingPropagandaOrScreeningPdfDTO.getSummaryType())){
            formSummary.setType(duLaYingPropagandaOrScreeningPdfDTO.getSummaryType());
        }
    }

    protected void afterInsertForSummary(Sqfw_formSummary_new formSummary) {
        sqfwServicerecordService.afterInsertForSummary(formSummary);
    }
}